# Maintainer: David Runge <dvzrv@archlinux.org>

pkgname=solr
pkgver=9.1.1
pkgrel=1
pkgdesc="Open source enterprise search platform built on Apache Lucene"
arch=(any)
url="https://lucene.apache.org/solr/"
license=(Apache)
depends=(bash 'java-runtime=11')
makedepends=(gradle git groovy 'java-environment=11')
backup=(
  etc/$pkgname/server/jetty-http.xml
  etc/$pkgname/server/jetty-https.xml
  etc/$pkgname/server/jetty-ssl.xml
  etc/$pkgname/server/jetty.xml
  etc/$pkgname/server/webdefault.xml
  etc/$pkgname/$pkgname.in.sh
  etc/$pkgname/$pkgname.xml
  etc/$pkgname/zoo.cfg
)
source=(
  https://downloads.apache.org/$pkgname/$pkgname/$pkgver/$pkgname-$pkgver-src.tgz{,.asc}
  $pkgname.service
  $pkgname.sysusers
  $pkgname.tmpfiles
  support-reproducible-builds.patch
  skip-failing-test.patch
  skip-checks-for-git-repo.patch
  allow-using-system-gradle.patch
)
sha512sums=('3b1694268a2c17349a677b6e7e5150aaa695d7eac4f948ed00b4054d80e7aed1b4d564f239a2eecb8f59f029a0daf9777698b4467c2c8b230288a2fa981a8b03'
            'SKIP'
            'd9a5dcc7a30bf449abf9028bc4aa88e196c953d82b884661ec3191d4a06ec50b106c01ecdcd225e3526ab4f4ce5634d6dcd4a03f1e128fdd4c26febb1b742532'
            '06e5e40b96d2b6668790e4b166fc2867b9e694a2c72fd57eec702526e009b8b0495acbe16a5a27e259827477f4783ce87742f1f806254d8a2baec23b0b317058'
            '97252d1ba1e4e211a6b5a038981cbc9d6663a0d7a980b23484f838eebb2f8194571a3f34ea6b92ed0efd4b7f862e020b5e3e8478ad1de1c6832e232d0b20ff6d'
            '2f3d944a9b5d1983b745f8fab99ed083f411e374aeecf61b290bed50591c3d59a570856c414ded00bbf26862156b4ef8839719eb5b6fd30ecccad9d09c142844'
            '43267da8d3061ddb6a1e93166e049a1381fd926e7738d858be4eea65fc09def9edc9890e5b78f1a3445dae301cae358542cbb3ab1b5b805275834b77452fa9cc'
            'f7abe1507b7483988e1401ee10ab673a3ad5f47a26a1330f5eec001aa2d7bb698260f46aa59909b9df2d2b915a2b3b41c8b99d28b645ae1bee10731a13a6ead5'
            '25cdcc4a12353e530323e2e90a3793a1dbe834aa73c40812c02b3706de1ad53a2237ee5d81ea6e7c6aaaa738b0ffb322fcacab730df98f4b82a5b7da454ba1ce')
b2sums=('329bc462c9a5fc84cdcc9675f0a7503ca991a20e83cc326817f9a80ad9b8c3e09e7fd4448349b820841af782c2af8d87b6a971508c8fbc66fc94335616a11947'
        'SKIP'
        'f970329fad0358cb19af9b9337047612d5949af8aea2dc7acd6c8424ba494644940d22f46b98a9cf1362ea2e5d966b863907de35e7c0edc2e48f7b0ed00473b7'
        '60ff37059a4ab8362551518d56ee105e7d19199727605d5ad6f3236bd31dde1cc5fa37ffa37009820ee3115da36ae64df4754454cef1db51d1c13cac039245cb'
        '37ab5d9af1da1178fcc58ce39654fdecb842b24d4e2264a2eec95c10223e7d003f27a6b2957a267a17ce0fa72258d96642d5b909963576a5a7d13e4e05c47a51'
        '0461317bffddbcc6438efcb611283628863bb6af852ceeb0a369bad7105010d12dbdf5e9b78bfb838500d019b1c2e9d588c5620450385cd6933edb2946865a11'
        '4aa232c056725eb7fac58d2ba1531209c706e8a3a6982c7cc9ad214b8105ef0950d872f272c4b3be5e4145dee1d2185ca6840d796b01d973219508ca31352ba0'
        '32ab4ad7b66ddcfaf946190e8f1a956060783108d5f079f5888aa409d5fe42cadb5fcb151857bd23a938bc00b10008d396f4def46a696340ef362794846ceba0'
        '564cd7389806a2fbc84985ca954eb46b65c61fe219add88137f6047d2d6acea3c188e6597d7d2332f0fe9892d1a7ebcc506fb67b649e90a3e5143525a3ff0836')
# list of trusted signing keys: https://downloads.apache.org/solr/KEYS
validpgpkeys=(
  2085660D9C1FCCACC4A479A3BF160FF14992A24C  # Ishan Chattopadhyaya <ishan@apache.org>
  E58A6F4D5B2B48AC66D5E53BD4F181881A42F9E6  # Ignacio Vera (CODE SIGNING KEY) <ivera@apache.org>
  81D3EB0408B4E1EB10AF443BA4F4C886B29BC2F4  # Alan Woodward (CODE SIGNING KEY) <romseygeek@apache.org>
  86EDB9C33B8517228E88A8F93E48C0C6EF362B9E  # Mike Drob (CODE SIGNING KEY) <mdrob@apache.org>
  38DA0C3CE8181703A08E4D57377C3BA26AD29C0A  # Bruno Roustant <broustant@apache.org>
  50E3EE1C91C7E0CB4DFB007B369424FC98F3F6EC  # Houston Paul Putman IV (CODE SIGNING KEY) <houston@apache.org>
  902CC51935C140BF820230961FD5295281436075  # Jason Gerlowski (CODE SIGNING KEY) <gerlowskija@apache.org>
  7D8D90F8F64F23077AC87CF7CB77CB79928BB0EC  # Atri Sharma <atri@apache.org>
  CFCE5FBB920C3C745CEEE084C38FF5EC3FCFDB3E  # Noble Paul (CODE SIGNING KEY) <noble@apache.org>
  FBC25D7E1712025294FE66590A6AA179B9BBF45E  # Timothy Potter (CODE SIGNING KEY) <thelabdude@apache.org>
  9722F25F650057E26C803B60A6D064D833B3A969  # Mayya Sharipova (CODE SIGNING KEY) <mayya@apache.org>
  E6E21FFCDCEA14C95910EA65051A0FAF76BC6507  # Adrien Grand (CODE SIGNING KEY) <jpountz@apache.org>
  3558857D1F5754B78C7F8B5A71A45A3D0D8D0B93  # Jan Høydahl (CODE SIGNING KEY) <janhoy@apache.org>
  C3E7CBD9B9FE2B419EB14B89612B39A5BC981763  # Michael A. Gibney <magibney@apache.org>
)

prepare() {
  cd $pkgname-$pkgver
  patch -Np1 -i "$srcdir"/support-reproducible-builds.patch
  patch -Np1 -i "$srcdir"/skip-failing-test.patch
  patch -Np1 -i "$srcdir"/skip-checks-for-git-repo.patch
  patch -Np1 -i "$srcdir"/allow-using-system-gradle.patch
}

build() {
  cd $pkgname-$pkgver

  # override adding -SNAPSHOT on version
  # skip signing of jars
  # skip generation of reference guide
  /usr/bin/gradle \
    -Dversion.release=$pkgver \
    assembleRelease \
    -Psign=false -x signJarsPublication \
    -Prefguide.include=false

  install -vdm 755 "$srcdir/$pkgname-$pkgver/dist"
  tar -xf "$pkgname/distribution/build/release/$pkgname-$pkgver.tgz" --strip 1 -C "$srcdir/$pkgname-$pkgver/dist"
}

check() {
  cd $pkgname-$pkgver

  /usr/bin/gradle \
    -Dversion.release=$pkgver \
    test \
    -Prefguide.include=false
}

package() {
  local config

  cd $pkgname-$pkgver

  # remove unneeded and OS specific scripts
  rm -rvf dist/bin/{install_solr_service.sh,solr.cmd,solr.in.cmd}

  # make target script executable
  chmod 755 dist/bin/${pkgname}
  # symlink script into PATH
  install -vdm 755 "$pkgdir/usr/bin/"
  ln -sv /usr/share/$pkgname/bin/$pkgname "$pkgdir/usr/bin/$pkgname"

  # configuration
  install -vDm 644 dist/bin/$pkgname.in.sh -t "${pkgdir}/etc/$pkgname"
  install -vDm 644 dist/server/etc/*.xml -t "${pkgdir}/etc/$pkgname/server"
  install -vDm 644 dist/server/$pkgname/$pkgname.xml -t "$pkgdir/etc/$pkgname/"
  install -vDm 644 dist/server/$pkgname/zoo.cfg -t "$pkgdir/etc/$pkgname/"

  # copy application
  install -vdm 755 "$pkgdir/usr/share/$pkgname/"
  cp -dr --no-preserve='ownership' dist/{bin,docs,example,licenses,modules,server} "$pkgdir/usr/share/$pkgname/"

  # symlink configuration into place
  ln -svf /etc/$pkgname/$pkgname.in.sh "$pkgdir/usr/share/$pkgname/"

  install -vdm 750 "$pkgdir/var/lib/$pkgname"
  ln -svf /etc/$pkgname/$pkgname.xml "$pkgdir/var/lib/$pkgname/"
  ln -svf /etc/$pkgname/zoo.cfg "$pkgdir/var/lib/$pkgname/"
  for config in {jetty,jetty-{http,https,ssl},webdefault}.xml; do
    ln -svf /etc/$pkgname/server/$config "$pkgdir/usr/share/$pkgname/server/etc/$config"
  done

  # logs directory
  install -vdm 750 "$pkgdir/var/log/$pkgname/"
  # docs
  install -vDm 644 dist/{CHANGES,NOTICE}.txt -t "$pkgdir/usr/share/doc/$pkgname/"
  install -vDm 644 README.md -t "$pkgdir/usr/share/doc/$pkgname/"
  install -vDm 644 ../$pkgname.service -t "$pkgdir/usr/lib/systemd/system/"
  install -vDm 644 ../$pkgname.sysusers "$pkgdir/usr/lib/sysusers.d/$pkgname.conf"
  install -vDm 644 ../$pkgname.tmpfiles "$pkgdir/usr/lib/tmpfiles.d/$pkgname.conf"
}
